﻿using System;
using System.Globalization;
using System.Text;

// This code was taken from CRM product assemblies that were disassembled.
namespace CRM2011PluginUtilities
{
    internal sealed class FullNameGenerator
    {
        public enum FullNameConventionCode
        {
            LastFirst,
            FirstLast,
            LastFirstMiddleInitial,
            FirstMiddleInitialLast,
            LastFirstMiddle,
            FirstMiddleLast,
            LastSpaceFirst,
            LastNoSpaceFirst
        }

        // Microsoft.Crm.BusinessEntities.FullNameGenerator
        public static string GenerateFullName(FullNameConventionCode fullNameConventionCode, string firstName, string middleName, string lastName)
        {
            firstName = FullNameGenerator.ConvertNullToEmptyString(firstName);
            middleName = FullNameGenerator.ConvertNullToEmptyString(middleName);
            lastName = FullNameGenerator.ConvertNullToEmptyString(lastName);
            string text = string.Empty;
            if (middleName.Length > 0)
            {
                text = middleName.Substring(0, 1);
            }
            StringBuilder stringBuilder = new StringBuilder();
            switch (fullNameConventionCode)
            {
                case FullNameConventionCode.LastFirst:
                    {
                        if (lastName.Length > 0 && firstName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}, {1}", new object[]
                            {
                                lastName, 
                                firstName
                            });
                        }
                        else
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0} {1}", new object[]
                            {
                                   lastName, 
                                   firstName
                            });
                        }
                        break;
                    }
                case FullNameConventionCode.FirstLast:
                    {
                        stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0} {1}", new object[]
                        {
                            firstName, 
                            lastName
                         });
                        break;
                    }
                case FullNameConventionCode.LastFirstMiddleInitial:
                    {
                        if (lastName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0},", new object[]
                            {
                                   lastName
                            });
                        }
                        if (firstName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[]
                            {
                                   firstName
                            });
                        }
                        if (middleName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}.", new object[]
                            {
                                   text
                            });
                        }
                        break;
                    }
                case FullNameConventionCode.FirstMiddleInitialLast:
                    {
                        stringBuilder.Append(firstName);
                        if (middleName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}.", new object[]
                            {
                                   text
                            });
                        }
                        stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[]
                        {
                            lastName
                        });
                        break;
                    }
                case FullNameConventionCode.LastFirstMiddle:
                    {
                        if (lastName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0},", new object[]
                            {
                                   lastName
                            });
                        }
                        if (firstName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[]
                            {
                                   firstName
                            });
                        }
                        if (middleName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[]
                            {
                                   middleName
                            });
                        }
                        break;
                    }
                case FullNameConventionCode.FirstMiddleLast:
                    {
                        stringBuilder.Append(firstName);
                        if (middleName.Length > 0)
                        {
                            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[]
                            {
                                   middleName
                            });
                        }
                        stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[]
                     {
                            lastName
                     });
                        break;
                    }
                case FullNameConventionCode.LastSpaceFirst:
                    {
                        stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0} {1}", new object[]
                     {
                            lastName, 
                            firstName
                     });
                        break;
                    }
                case FullNameConventionCode.LastNoSpaceFirst:
                    {
                        stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}", new object[]
                        {
                            lastName, 
                            firstName
                        });
                        break;
                    }
                default:
                    {
                        throw new ArgumentOutOfRangeException("fullNameConventionCode");
                    }
            }
            return stringBuilder.ToString().Trim();
        }

        private static string ConvertNullToEmptyString(string name)
        {
            if (name == null)
            {
                return string.Empty;
            }
            return name.Trim();
        }
    }
}
